package View;

import java.awt.Point;
import java.util.List;
import java.util.Map;

import Actor.Actor;
import Main.Constant;
import Map.Floor;
import Map.EnumTile;
import Map.Tile;

public class ViewManager {
	private final Floor tile;
	List<Actor> actors;
	List<Actor> monster;
	Map<List<Point>, Integer> bullets;
	private int viewX = 0;
	private int viewY = 0;
	
	
	private int testx = 0;
	private int testy = 0;
	Map<List<Point>, Integer> bullets1;
	public ViewManager(Floor _tile, List<Actor> _actor,
			List<Actor> _monster, Map<List<Point>, Integer> _bullets, Map<List<Point>, Integer> _bullets1) {
		tile = _tile;
		actors = _actor;
		monster = _monster;
		bullets = _bullets;
		bullets1=_bullets1;
	}
	public DrawObject getTile() {
		//System.out.println(viewX+","+viewY);
		//System.out.println(testx+","+testy);
		Tile[][] tmpTile= tile.getMap(getTesty()/10, getTestx()/10, Constant.VIEWSIZE, Constant.VIEWSIZE);



		for (Actor mon : monster) {
			if (mon.getY() > getTesty()/10
					&& mon.getY() - getTesty()/10 < Constant.VIEWSIZE
					&& mon.getX() > getTestx()/10
					&& mon.getX() - getTestx()/10 < Constant.VIEWSIZE) {
				try {

					tmpTile[mon.getY() - getTesty()/10][mon.getX() - getTestx()/10] = new Tile(EnumTile.ACTOR);

				} catch (Exception e) {

				}
			}
		}
		for (List<Point> list : bullets.keySet()) {
			// path
			for (Point a : list) {
				if (a.y > getTesty()/10 && a.y - getTesty()/10 < Constant.VIEWSIZE
						&& a.x > getTestx()/10
						&& a.x - getTestx()/10 < Constant.VIEWSIZE) {
					try {
						tmpTile[a.y - getTesty()/10][a.x - getTestx()/10] = new Tile(
								EnumTile.PATH);
					} catch (Exception e) {

					}
				}
			}
			// bullet
			Integer i = bullets.get(list);
			try {
				Point pts = list.get(i);
				if (pts.y > getTesty()/10
						&& pts.y - getTesty()/10 < Constant.VIEWSIZE
						&& pts.x > getTestx()/10
						&& pts.x - getTestx()/10 < Constant.VIEWSIZE) {
					try {
						tmpTile[pts.y - getTesty()/10][pts.x - getTestx()/10] = new Tile(
								EnumTile.BULLET);
					} catch (Exception e) {

					}
				}
			} catch (Exception e) {

			}
		}
		DrawObject drawObject = new DrawObject();
		drawObject.tile = tmpTile;
		drawObject.actors = actors;
		drawObject.monster = monster;
		drawObject.continent = tile;
		return drawObject;
	}
//	public DrawObject getTile(int n) {
//		System.out.println("viewX,viewY: "+viewX+","+viewY);
//		System.out.println("testx,testy: "+testx+","+testy);
//		Tile[][] tmpTile= tile.getMap(getViewY(), getViewX(), Constant.VIEWSIZE, Constant.VIEWSIZE);
//
//
//
//		for (Actor a : monster) {
//			if (a.getY() > getViewY()
//					&& a.getY() - getViewY() < Constant.VIEWSIZE
//					&& a.getX() > getViewX()
//					&& a.getX() - getViewX() < Constant.VIEWSIZE) {
//				try {
//
//					tmpTile[a.getY() - getViewY()][a.getX() - getViewX()] = new Tile(EnumTile.ACTOR);
//
//				} catch (Exception e) {
//
//				}
//			}
//		}
//		for (List<Point> list : bullets.keySet()) {
//			// path
//			for (Point a : list) {
//				if (a.y > getViewY() && a.y - getViewY() < Constant.VIEWSIZE
//						&& a.x > getViewX()
//						&& a.x - getViewX() < Constant.VIEWSIZE) {
//					try {
//						tmpTile[a.y - getViewY()][a.x - getViewX()] = new Tile(
//								EnumTile.PATH);
//					} catch (Exception e) {
//
//					}
//				}
//			}
//			// bullet
//			Integer i = bullets.get(list);
//			try {
//				Point pts = list.get(i);
//				if (pts.y > getViewY()
//						&& pts.y - getViewY() < Constant.VIEWSIZE
//						&& pts.x > getViewX()
//						&& pts.x - getViewX() < Constant.VIEWSIZE) {
//					try {
//						tmpTile[pts.y - getViewY()][pts.x - getViewX()] = new Tile(
//								EnumTile.BULLET);
//					} catch (Exception e) {
//
//					}
//				}
//			} catch (Exception e) {
//
//			}
//		}
//		DrawObject drawObject = new DrawObject();
//		drawObject.tile = tmpTile;
//		drawObject.actors = actors;
//		drawObject.monster = monster;
//		return drawObject;
//	}

	public void doUpLeftCommand() { // up

//		if ((actors.get(0).getY() / 10 - getViewY() < Constant.VIEWBOUNDARY || actors
//				.get(0).getX() / 10 - getViewX() < Constant.VIEWBOUNDARY)) {
//			if (getViewY() > 0)
//				setViewY(getViewY() - 1);
//			if (getViewX() > 0)
//				setViewX(getViewX() - 1);
//
//
//		}
		
		
		if ((actors.get(0).getY()  - getTesty() < Constant.VIEWBOUNDARY*10 || actors
				.get(0).getX() - getTestx() < Constant.VIEWBOUNDARY*10)) {

			
			if (getTesty() > 0)
				setTesty(getTesty() - Actor.displacement);
			if (getTestx() > 0)
				setTestx(getTestx() - Actor.displacement);

		}
	}

	public void doDownRightCommand() { // down

//		if ((actors.get(0).getY() / 10 - getViewY()) > Constant.VIEWSIZE
//				- Constant.VIEWBOUNDARY
//				|| (actors.get(0).getX() / 10 - getViewX() > Constant.VIEWSIZE
//						- Constant.VIEWBOUNDARY)) {
//			setViewY(getViewY() + 1);
//			setViewX(getViewX() + 1);
//			
//		}
		
		
		
		if ((actors.get(0).getY()  - getTesty()) > (Constant.VIEWSIZE
				- Constant.VIEWBOUNDARY)*10
				|| (actors.get(0).getX()  - getTestx() > (Constant.VIEWSIZE
						- Constant.VIEWBOUNDARY)*10)) {

			
			setTesty(getTesty() + Actor.displacement);
			setTestx(getTestx() + Actor.displacement);
		}
	}

	public void doUpRightCommand() { // right

//		if ((actors.get(0).getY() / 10 - getViewY() < 0 + Constant.VIEWBOUNDARY)
//				|| (actors.get(0).getX() / 10 - getViewX() > Constant.VIEWSIZE
//						- Constant.VIEWBOUNDARY)) {
//			setViewY(getViewY() - 1);
//			setViewX(getViewX() + 1);
//
//		}
		
		
		
		if ((actors.get(0).getY()  - getTesty() < 0 + Constant.VIEWBOUNDARY*10)
				|| (actors.get(0).getX()  - getTestx() > (Constant.VIEWSIZE
						- Constant.VIEWBOUNDARY)*10)) {

			setTesty(getTesty() - Actor.displacement);
			setTestx(getTestx() + Actor.displacement);
		}

	}

	public void doDownLeftCommand() { // left

//		if ((actors.get(0).getX() / 10 - getViewX() < 0 + Constant.VIEWBOUNDARY)
//				|| (actors.get(0).getY() / 10 - getViewY() > Constant.VIEWSIZE
//						- Constant.VIEWBOUNDARY)) {
//			setViewY(getViewY() + 1);
//			setViewX(getViewX() - 1);
//			
//
//		}
		
		
		
		if ((actors.get(0).getX()  - getTestx() < 0 + Constant.VIEWBOUNDARY*10)
				|| (actors.get(0).getY()  - getTesty() >( Constant.VIEWSIZE
						- Constant.VIEWBOUNDARY)*10)) {

			setTesty(getTesty() + Actor.displacement);
			setTestx(getTestx() - Actor.displacement);
		}
	}

//	public void setViewX(int viewX) {
//		this.viewX = viewX;
//	}
//
//	public int getViewX() {
//		return viewX;
//	}
//
//	public void setViewY(int viewY) {
//		this.viewY = viewY;
//	}
//
//	public int getViewY() {
//		return viewY;
//	}

	public int getTestx() {
		return testx;
	}

	public void setTestx(int testx) {
		this.testx = testx;
	}

	public int getTesty() {
		return testy;
	}

	public void setTesty(int testy) {
		this.testy = testy;
	}

}
